View Javadoc

1   package org.naftulin.classpathexplorer.dublicate.imlp;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.Enumeration;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.zip.ZipEntry;
9   import java.util.zip.ZipFile;
10  
11  import org.naftulin.classpathexplorer.AccessibleResource;
12  import org.naftulin.logwrapper.LogAdapter;
13  import org.naftulin.logwrapper.LogLevelAdaptor;
14  import org.naftulin.timespan.TimeSpan;
15  
16  
17  /***
18   * Represents a Zip - archive for accessible resources.
19   * @author henry naftulin
20   * @version 1.0
21   */
22  class ZipArchive extends AccessibleArchive {
23  	private static LogAdapter log = LogAdapter.getLogger(ZipArchive.class);
24  	
25  	ZipArchive(File file) {
26  		super(file);
27  	}
28  
29  	/***
30  	 * Returns all of the resources in this archive.
31  	 * @return all of the resources in this archive.
32  	 * @throws IOException if an error occurs while reading the content of 
33  	 *  the archive.
34  	 */
35  	public AccessibleResource[] getAccessibleResources() throws IOException {
36  		TimeSpan readTime = new TimeSpan("reading resource from " + getPath());
37  		readTime.start();
38  		ZipFile file = new ZipFile(getPath());
39  		List resources = new LinkedList();
40  		Enumeration fileEnum = file.entries();
41  		while(fileEnum.hasMoreElements()) {
42  			ZipEntry entry = (ZipEntry) fileEnum.nextElement();
43  			if (!entry.isDirectory()) {
44  				resources.add(new AccessibleResourceImpl(entry, this));
45  			}
46  		}
47  		
48  		// TODO Auto-generated method stub
49  		AccessibleResource[] accessibleResources = new AccessibleResource[resources.size()];
50  		accessibleResources = (AccessibleResource[]) resources.toArray(accessibleResources);
51  		readTime.stop();
52  		readTime.log(log, LogLevelAdaptor.DEBUG);
53  		return accessibleResources;
54  	}
55  	
56  	
57  	/***
58  	 * Returns the string representation of the resource.
59  	 * @return the string representation of the resource.
60  	 */
61  	public String toString() {
62  		StringBuffer sb = new StringBuffer(100);
63  		sb.append("ZipArchive[");
64  		sb.append(super.toString());
65  		sb.append("]\n");
66  		return sb.toString();
67  	}
68  	
69  	/***
70  	 * Returns the xml representation of the resource.
71  	 * @return the xml representation of the resource.
72  	 */
73  	public String toXml() {
74  		StringBuffer sb = new StringBuffer(100);
75  		sb.append("<zipArchive ");
76  		sb.append(super.toString());
77  		sb.append("/>\n");
78  		return sb.toString();
79  	}
80  }